package org.example.journal_items.controller;

import org.example.journal_items.Entity.Diary;
import org.example.journal_items.service.DiaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.example.journal_items.security.CustomUserDetails;
import org.example.journal_items.Entity.User;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.example.journal_items.common.Result;

import java.util.Map;
import java.util.UUID;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import jakarta.persistence.EntityNotFoundException;

@RestController
@RequestMapping("/diaries")
public class DiaryController {

  @Autowired
  private DiaryService diaryService;

  // 获取当前用户的日记列表（分页）
  @GetMapping
  public Result<Page<Diary>> getDiaries(
      @AuthenticationPrincipal CustomUserDetails userDetails,
      @RequestParam(defaultValue = "0") int page,
      @RequestParam(defaultValue = "10") int size) {
    User user = userDetails.getUser();
    Page<Diary> diaries = diaryService.getDiariesByUserId(user.getId(), page, size);
    return Result.success(diaries);
  }

  // 创建新日记
  @PostMapping
  public Result<Diary> createDiary(@AuthenticationPrincipal CustomUserDetails userDetails,
      @RequestBody Diary diary) {
    User user = userDetails.getUser();
    diary.setUserId(user.getId());
    Diary createdDiary = diaryService.createDiary(diary);
    return Result.success(createdDiary);
  }

  // 获取单篇日记详情
  @GetMapping("/{id}")
  public Result<Diary> getDiaryById(@PathVariable Long id, @AuthenticationPrincipal CustomUserDetails userDetails) {
    try {
      User user = userDetails.getUser();
      Diary diary = diaryService.getDiaryById(id);

      // 检查权限：只能查看自己的日记
      if (!diary.getUserId().equals(user.getId())) {
        return Result.error("没有权限查看此日记");
      }

      return Result.success(diary);
    } catch (EntityNotFoundException e) {
      return Result.error("日记不存在");
    } catch (Exception e) {
      return Result.error("获取日记详情失败: " + e.getMessage());
    }
  }

  // 更新日记
  @PutMapping("/{id}")
  public Result<Diary> updateDiary(@PathVariable Long id, @RequestBody Diary diaryDetails) {
    Diary updatedDiary = diaryService.updateDiary(id, diaryDetails);
    return Result.success(updatedDiary);
  }

  // 删除日记
  @DeleteMapping("/{id}")
  public Result<Void> deleteDiary(@PathVariable Long id) {
    diaryService.deleteDiary(id);
    return Result.success(null);
  }

  // 切换日记收藏状态
  @PostMapping("/{id}/favorite")
  public Result<Diary> toggleFavorite(@PathVariable Long id) {
    Diary updatedDiary = diaryService.toggleFavorite(id);
    return Result.success(updatedDiary);
  }

  // 获取当前用户的私密日记列表
  @GetMapping("/private")
  public Result<java.util.List<Diary>> getPrivateDiaries(@AuthenticationPrincipal CustomUserDetails userDetails) {
    User user = userDetails.getUser();
    java.util.List<Diary> list = diaryService.getPrivateDiaries(user.getId());
    return Result.success(list);
  }
}
